<template>
  <div class="app-container">
    <!-- 搜索部分-->
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="姓名" prop="name">
        <el-input
          v-model.trim="queryParams.name"
          placeholder="请输入姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
<!--      <el-form-item label="曾任职岗位" prop="previousPosition">-->
<!--        <el-input-->
<!--          v-model.trim="queryParams.previousPosition"-->
<!--          placeholder="请输入曾任职岗位"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="入职时间" prop="startTime">-->
<!--        <el-date-picker v-model.trim="queryParams.startTime"-->
<!--                        clearable-->
<!--                        type="date"-->
<!--                        value-format="yyyy-MM-dd"-->
<!--                        placeholder="请选择起始时间">-->
<!--        </el-date-picker>-->
<!--      </el-form-item>-->
<!--      <el-form-item label="离职时间" prop="endTime">-->
<!--        <el-date-picker clearable-->
<!--                        v-model.trim="queryParams.endTime"-->
<!--                        type="date"-->
<!--                        value-format="yyyy-MM-dd"-->
<!--                        placeholder="请选择离职时间">-->
<!--        </el-date-picker>-->
<!--      </el-form-item>-->
<!--      <el-form-item label="离职原因" prop="reasonsForLeaving">-->
<!--        <el-input-->
<!--          v-model.trim="queryParams.reasonsForLeaving"-->
<!--          placeholder="请输入离职原因"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
      <el-form-item style="margin-left: 20px">
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- 按钮部分-->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['module:history:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['module:history:edit']"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['module:history:remove']"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['module:history:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList" :columns="columns"></right-toolbar>
    </el-row>

    <!-- 列表部分-->
    <el-table :border="true" v-loading="loading" :data="historyList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <!--      <el-table-column label="昵称" align="center" prop="username"/>-->
      <el-table-column label="姓名" align="center" prop="name" v-if="columns[0].visible"/>
      <el-table-column label="曾任职公司" align="center" prop="previousCompany" v-if="columns[1].visible"/>
      <el-table-column label="曾任职岗位" align="center" prop="previousPosition" v-if="columns[2].visible"/>
      <el-table-column label="起始时间" align="center" prop="startTime" width="180" v-if="columns[3].visible">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.startTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="离职时间" align="center" prop="endTime" width="180" v-if="columns[4].visible">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.endTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="离职原因" align="center" prop="reasonsForLeaving" v-if="columns[5].visible"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['module:history:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['module:history:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页器部分-->
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改员工工作经历对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="50%" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" :inline="true" label-width="200px">

        <el-form-item label="姓名" prop="name">
          <el-input style="width: 200px" v-model="form.name" readonly placeholder="请选择员工">
            <template slot="append">
              <el-button type="primary" @click="openImportUser2">
                选择员工
              </el-button>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item label="曾任职公司" prop="previousCompany">
          <el-input v-model="form.previousCompany" placeholder="请输入曾任职公司" style="width: 200px"/>
        </el-form-item>
        <el-form-item label="曾任职岗位" prop="previousPosition">
          <el-input v-model="form.previousPosition" placeholder="请输入曾任职岗位" style="width: 200px"/>
        </el-form-item>
        <!--        <el-form-item label="删除标识" prop="delFlag">-->
        <!--          <el-input disabled="" v-model="form.delFlag" style="width: 200px"/>-->
        <!--        </el-form-item>-->
        <el-form-item label="起始时间" prop="startTime">
          <el-date-picker clearable
                          v-model="form.startTime"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="请选择起始时间"
                          style="width: 200px">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="离职时间" prop="endTime">
          <el-date-picker clearable
                          v-model="form.endTime"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="请选择离职时间"
                          style="width: 200px">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="离职原因" prop="reasonsForLeaving">
          <el-input type="textarea" v-model="form.reasonsForLeaving" maxlength="200" show-word-limit
                    placeholder="请输入离职原因" style="width: 320px;"/>

        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <Elselect @meiye="myfn" @anme="anmefn" @chongzhi="czfn" @serch="serchfn" @dangqian="dqfn" ref="import" :list="staffList" ></Elselect>
  </div>
</template>

<script>
import {listHistory, getHistory, delHistory, addHistory, updateHistory} from "@/api/module/history";
import {listStaff} from "@/api/module/staff";
import Elselect from "@/components/ELselect/Elselect"
import imgUpload from "@/components/ImageUpload";

export default {
  name: "History",
  components: {
    Elselect,
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 员工工作经历表格数据
      historyList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        staffId: null,//员工id
        name: null,//姓名
        previousCompany: null,//曾任职公司
        previousPosition: null,//曾任职岗位
        startTime: null,//起始时间
        endTime: null,//离职时间
        reasonsForLeaving: null,//离职原因
        delFlag: null
      },
      // 表单参数
      form: {},
      columns: [
        { key: 0, label: `姓名`, visible: true },
        { key: 1, label: `曾任职公司`, visible: true },
        { key: 2, label: `曾任职岗位`, visible: true },
        { key: 3, label: `起始时间`, visible: true },
        { key: 4, label: `离职时间`, visible: true },
        { key: 5, label: `离职原因`, visible: true },

      ],
      // 表单校验
      rules: {
        staffId: [{required: true, message: '请输入员工id', trigger: 'blur'},],
        name: [{required: true, message: '请输入姓名', trigger: 'blur'},],
        previousCompany: [{required: true, message: '请输入曾任职公司', trigger: 'blur'},],
        startTime: [{required: true, message: '请选择起始时间', trigger: 'change'},],
        endTime: [{required: true, message: '请选择离职时间', trigger: 'change'},],
        previousPosition: [{required: true, message: '请输入曾任职岗位', trigger: 'blur'},],
        reasonsForLeaving: [{required: true, message: '请输入离职原因', trigger: 'blur'},],
      },
      staffList: [],// 名字组件传参
    };
  },
  created() {
    this.getList();
  },
  methods: {
    openImportUser2() {
      this.$refs.import.show();
    },
    fousfn(val) {
      this.form.name = val
    },

    /** 查询员工工作经历列表 */
    getList() {
      this.loading = true;
      listHistory(this.queryParams).then(response => {
        console.log(response)
        this.historyList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        staffId: null,
        name: null,
        previousCompany: null,
        previousPosition: null,
        startTime: null,
        endTime: null,
        reasonsForLeaving: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    async handleAdd() {
      this.reset();
      this.form.delFlag = 0//0代表存在 2代表删除
      this.form.username = this.$store.getters.name
      const res = await listStaff(this.queryParams)
      this.staffList = res.rows
      this.open = true;
      this.title = "添加员工工作经历";
      this.$refs.import.unit =res.total
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      console.log('修改行', row)

      this.reset();
      const id = row.id || this.ids
      getHistory(id).then(response => {
        console.log(response)
        this.form = response.data
        this.open = true;
        this.title = "修改员工工作经历";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateHistory(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addHistory(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除员工工作经历编号为"' + ids + '"的数据项？').then(function () {
        return delHistory(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('module/history/export', {
        ...this.queryParams
      }, `history_${new Date().getTime()}.xlsx`)
    },
    async myfn(val){
      console.log(val)
      this.queryParams.pageSize=val
      const res = await listStaff(this.queryParams)
      // console.log(res.rows)
      this.staffList=res.rows
    },
    async dqfn(val){
      this.queryParams.pageNum=val
      const res = await listStaff(this.queryParams)
      // console.log(res.rows)
      this.staffList=res.rows
    },
    //组件搜索功能
    async serchfn(val){
      this.queryParams.name=val
      const res = await listStaff(this.queryParams)
      this.staffList=res.rows
    },
    //组件重置功能
    async czfn(){
      this.queryParams = {
        pageNum: 1,
        pageSize: 10,
        username: null,
        trainingItems: null,
        sponsor: null,
        sponsorDept: null,
        participant: null,
        participantDept: null,
        initiateTime: null,
        startTime: null,
        endTime: null,
        days: null,
        status: null,
        tenantId: null,
        staffId: null,
        staffName: null,
      }
      const res = await listStaff(this.queryParams)
      this.staffList=res.rows
      this.listDatafn()
    },
    anmefn(val){
      this.form.staffName=val
      this.$refs.import.visible=false
    }

  }
};
</script>
